<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once($phpbb_root_path . 'common.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
require_once($phpbb_root_path . 'sv_common.' . $phpEx);
require_once($phpbb_root_path . 'fb/facebook.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');

$in_fb_iframe = true;

// save fb code for access token. We will receive the code only when the user allow us to get the basic user information.
$code = $_REQUEST['code'];

// As we are in FB iframe, check for FB users first. 
// Check if we can get the fb user id.
$fb_sig_user = $_REQUEST['fb_sig_user'];
if ($fb_sig_user != '')
{
  	// get fb user info.
   	$fb_user_info = file_get_contents("https://graph.facebook.com/$fb_sig_user");
	if ($fb_user_info)
	{
		$fb_user = json_decode($fb_user_info,true); // to make it an array  		
		
		if (login_fb_user($fb_user['id'], $fb_user['name'], $fb_user['first_name'], $fb_user['last_name'], '', $fb_user['link'], $fb_user['gender'], $fb_user['timezone'], $fb_user['locale'], $fb_user['verified']))
		{
			// refresh
			$url = append_sid("{$phpbb_root_path}/fb/index.$phpEx", '');
			redirect($url, false, true);
			echo "<a href='" . $url . "'>Redirecting...</a><br /><br />" . $url;
		}
	}
}

$show_user_value 	= false;
$show_1_month_roi 	= false;

$money = $user->data['user_money'];
$user_id 	= $user->data['user_id'];

if ($user_id != ANONYMOUS)
{
	// list all holding stock.
	// As we can only show 1 page in FB iframe, provide a url for the user to get detail.	
//	$detail_url	= append_sid("{$phpbb_root_path}index.$phpEx", '');
//	list_holding_stock($query_user_id, 3, htmlentities($detail_url));
	
	// The scroll bar of FB iframe works again, so we don't need to limit on 1 page.
	list_holding_stock($user_id);
	
	// list all open to_buy delegation. 
	list_open_to_buy_delegation();

	// list all open to_sell delegation. 
	list_open_to_sell_delegation();
	
	// we need to run this before we output page_footer, or this function will not be executed on stockvirtual.info(although it works on localhost).
	// update the user's fb friends. 
	$user_fb_id = get_user_fb_id($user_id);
	if ($user_fb_id != 0 && $code != '')
	{
		update_fb_friends($user_id, $user_fb_id, $code);	
	}
}
else
{
	// show ranking directly on index page if the user is not logged in.
	// list highest 20 user values in the latest date. 
	$show_user_value = list_user_value_ranking(10, 0, '', 0, true);

	// list highest 20 user ROI in 1 month period. 
	$show_1_month_roi = list_user_roi_ranking(30, 'roi_1_month_list', 10, 0, '', 0, true);
}

if (is_newbie($user_id))
{
	$show_welcome_newbie	= true;
	$show_rule 				= true;	
}

$show_statistics = false;
if ($auth->acl_getf_global('m_'))
{
	$show_statistics = true;
}

// knowledge
$show_knowledge = false;
if (is_knowledge_db_ok())
{
	$titles = get_knowledge_titles();

	if (sizeof($titles) > 0)
	{
		$show_knowledge = true;
		
		foreach($titles as $id => $title)
		{
			$template->assign_block_vars('knowledge_titles', array(
				'ID'							=> $id,
				'TITLE'							=> $title,
				)
			);
		}	
	}
}

display_forums('', $config['load_moderators']);

// Assign index specific vars
$template->assign_vars(array(

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'				=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_SV_INDEX'					=> append_sid("{$phpbb_root_path}fb/index.$phpEx", ''),
	'S_BUY_ACTION'					=> append_sid("{$phpbb_root_path}fb/buy.$phpEx", ''),
	'S_SELL_ACTION'					=> append_sid("{$phpbb_root_path}fb/sell.$phpEx", ''),
	'S_PERFORMANCE_ACTION'			=> append_sid("{$phpbb_root_path}fb/performance.$phpEx", ''),
	'S_HISTORY_ACTION'				=> append_sid("{$phpbb_root_path}fb/history.$phpEx", ''),
	'S_RANKING_ACTION'				=> append_sid("{$phpbb_root_path}fb/ranking.$phpEx", ''),
	'S_RULE'						=> append_sid("{$phpbb_root_path}fb/rule.$phpEx", ''),
	'S_STATISTICS'					=> append_sid("{$phpbb_root_path}fb/statistics.$phpEx", ''),
	'S_FB_FRIENDS_RANKING_ACTION'	=> append_sid("{$phpbb_root_path}fb/fb_friends_ranking.$phpEx", ''),
	'S_QUERY_USER'					=> append_sid("{$phpbb_root_path}fb/query_user.$phpEx", 'mode=query_user'),
	'S_FOLLOW_INFO'					=> append_sid("{$phpbb_root_path}fb/follow_info.$phpEx", ''),
	
	'S_FB_LOGIN_URL'			=> $loginUrl,
	
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,

	'S_TO_BUY_DELEGATION_LIST'	=> $to_buy_delegation_list,
	'S_TO_SELL_DELEGATION_LIST'	=> $to_sell_delegation_list,
	'S_HOLDING_STOCK_LIST'		=> $holding_stock_list,
	'U_MONEY'					=> number_format($money),
	'U_USERNAME'				=> $user->data['username'],
	
	'S_HIDE_NAV_LINK'				=> true,
	'S_SHOW_RULE'					=> $show_rule,
	'S_SHOW_WELCOME_NEWBIE'			=> $show_welcome_newbie,
	'S_SHOW_STATISTICS'				=> $show_statistics,
	'S_HAS_FB_FRIEND_REGISTERED'	=> true,
	'S_SHOW_KNOWLEDGE'				=> $show_knowledge,
	
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '',
	
	'IN_FB_IFRAME'		=> $in_fb_iframe,
	
	'SHOW_USER_VALUE'	=>	$show_user_value,
	'SHOW_1_MONTH_ROI'	=>	$show_1_month_roi,
	
	'IS_FOLLOWING_ANYONE'	=>	$is_following_anyone,
	)
);

// Output page
page_header($user->lang['INDEX']);

$template->set_filenames(array(
	'body' => 'index_body.html')
);

page_footer();
?>